package com.dev.op.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dev.op.entity.Submission;
import com.dev.op.entity.User;
import com.dev.op.mapper.UserMapper;
import com.dev.op.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User login(String emailOrPhoneNum, String password) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper
                .eq(User::getPhoneNumber, emailOrPhoneNum)
                .or()
                .eq(User::getEmail, emailOrPhoneNum)
        );
        User user =  this.getOne(queryWrapper);
        if(user != null){
            if(!user.getPassword().equals(password)){
                user = null;
            }
            else{
                user.setPassword(null);
            }
        }
        return user;
    }

    @Override
    public Page<User> getUsersWithoutPassword(int page, int size) {
        Page<User> userPage = new Page<>(page, size);
        // 执行分页查询，返回分页结果
        Page<User> resultPage = userMapper.selectPage(userPage,null);
        resultPage.getRecords().forEach(user -> user.setPassword(null));
        return resultPage;
    }
}
